<template>
  <div>
    <el-dialog
      title="更新流程"
      :visible="visible"
      width="40%"
      @close="visible = false"
      :close-on-click-modal="false"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="100px"
        class="demo-form"
        label-position="right"
        size="mini"
        style="width: 80%"
        v-loading="loading"
      >
        <el-form-item label="名称" prop="name" :label-width="defaultLabelWidth">
          <el-input disabled size="mini" type="text" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="des" :label-width="defaultLabelWidth">
          <el-input size="mini" type="textarea" v-model="form.des"></el-input>
        </el-form-item>
        <el-form-item label="版本" prop="workflowId.versionId" :label-width="defaultLabelWidth">
          <el-input size="mini" type="number" v-model="form.workflowId.versionId"></el-input>
        </el-form-item>
        <el-form-item
          label="版本描述"
          prop="versionDescription"
          :label-width="defaultLabelWidth"
        >
          <el-input
            size="mini"
            type="textarea"
            v-model="form.versionDescription"
          ></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="confirm" size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      id: 1,
    },
  },
  watch: {
    value: {
      handler() {
        this.load();
      },
      deep: true,
    },
    visible(newVal, oldVal) {
      if (!newVal && oldVal) this.hide();
    },
  },
  model: {
    prop: "value",
    event: "change",
  },
  data() {
    return {
      loading: false,
      visible: false,
      defaultLabelWidth: "150px",
      form: {workflowId:{}},
      rules: {
        name: [{ required: true, message: "请输入", trigger: "change" }],
        "workflowId.versionId": [{ required: true, message: "请输入", trigger: "change" }],
      },
    };
  },
  methods: {
    confirm() {
      this.commit();
    },
    show(value) {
      this.visible = true;
      if (value) this.loadData(value);
    },
    hide() {
      this.visible = false;
      // this.form = {
      //   name: ""
      // };
    },
    loadData(value) {
      this.loading = true;
      Object.keys(value).forEach((key) => {
        this.$set(this.form, key, value[key]);
      });
      this.loading = false;
    },
    commit() {
      const _this = this;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.workflowId.versionId=Number( this.form.workflowId.versionId)
          this.$store
            .dispatch("workflow/updateWorkflow", this.form)
            .then((res) => {
              this.$emit("ok-click", res);
              this.hide();
              this.$message({
                message: "操作成功！",
                type: "success",
              });
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style></style>
